package com.umpay.junit.test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.springframework.test.util.AssertionErrors.fail;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.ModelAndView;

import com.umfwechat.h5.controller.TestController;
import com.umfwechat.h5.controller.cashier.CashierController;
import com.umfwechat.util.PropertyUtil;


/**
 * Created by hopeone on 2017/5/11.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-context.xml"})
public class JunitTestController {

    @Resource
    TestController ttestController;
    
    @Resource
    CashierController cashierController;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private MockHttpSession session;

    static{
    	/**
    	 * 
    	 */
    	String url = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","junit.test.url", ""); 
    	System.setProperty("resin.home", url);
    }


    @Before
    public void before()
    {
    	/**
    	 * 设置resin_home环境变量
    	 */
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        session = new MockHttpSession();
        request.setCharacterEncoding("UTF-8");
        //此处初始化session即可
        
    }

    @Test
    public void test()
    {
        //request.setRequestURI("createProduct.htm");
        //request.setMethod(HttpMethod.POST.name());

        ModelAndView mv = null;
        try
        {
            mv = ttestController.test(request, response);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            fail("testToSearchPage failed!");
        }

        assertNotNull(mv);
        assertEquals(response.getStatus(), 200);
    }

    @Test
    public void jsontest()
    {

        try
        {
        	HttpSession session = null;
        	HttpServletRequest request =new MockHttpServletRequest();
        	HttpServletResponse response = new MockHttpServletResponse();
        	session = request.getSession();
        	
           String res = cashierController.getcashierList(session,request,response);
           System.out.println("res="+res);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            fail("testToSearchPage failed!");
        }


        assertEquals(response.getStatus(), 200);
    }
}
